package com.pengheng.example1;

import cn.hutool.core.util.HexUtil;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.DERUTF8String;

import java.io.IOException;

public class Example {
    public static void main(String[] args) throws IOException {
        // 创建一个 Person 对象
        Person person = new Person(
                new DERUTF8String("Tom"),
                new ASN1Integer(20),
                new DERUTF8String("Beijing")
        );

        // 对 Person 对象进行编码
        byte[] encodedPerson = person.toASN1Primitive().getEncoded();

        // 对 Person 对象进行解码
        try {
            ASN1Sequence seq = ASN1Sequence.getInstance(encodedPerson);
            Person decodedPerson = Person.getInstance(seq);

            System.out.println("person的编码数据为：");
            System.out.println(HexUtil.encodeHexStr(encodedPerson));
            System.out.println("person的解码数据为：");
            System.out.println("name: " + decodedPerson.getName().getString());
            System.out.println("age: " + decodedPerson.getAge().getValue());
            System.out.println("address: " + (decodedPerson.getAddress() == null ? "null" : decodedPerson.getAddress().getString()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}